class csns::dgyumlocal(
$mirrorserver= "202.122.33.67",
$majorver= $facts['os']['release']['major'],
$nminorrelease = false,
){
}


class csns::dgyumlocal::purge(
  $purgerepo=true,
){
  if $purgerepo {
    resources { 'yumrepo':
      purge => true,
      before => Class[csns::dgyumlocal::repos_sl],
    }
  }

}

class csns::dgyumlocal::repos_sl(
  $osuselocal      = true,
  $sitepriority    = "10",
  $yumautoupdate   = false,
  $epelgrid        = false,
  $epelyum         = true,
  $enable_debug    = true,
  $enable_afs      = true,
) inherits csns::dgyumlocal {

if $yumautoupdate {
  $apply_updates = "yes"
}
else {
  $apply_updates = "no"
}

if $facts['os']['architecture'] == 'x86_64' {
  if $osuselocal {
    $centos_media="centos-local"
  }
  else{
    $centos_media="centos"
  }
}
else {
  $centos_media="centos-altarch"
}


case $operatingsystem {
/AlmaLinux|almalinux/:{
  yumrepo {"alma-baseos":
    baseurl  => "http://${mirrorserver}/almalinux/${majorver}/BaseOS/\$basearch/os/",
    descr    => 'AlmaLinux $releasever - BaseOS',
    enabled  => 1,
    gpgcheck => 0,
    priority => 20,
    before   => Yumrepo['site']
  }
  yumrepo {"alma-appstream":
    baseurl  => "http://${mirrorserver}/almalinux/${majorver}/AppStream/\$basearch/os/",
    descr    => 'AlmaLinux $releasever - AppStream',
    enabled  => 1,
    gpgcheck => 0,
    priority => 20,
    before   => Yumrepo['site']
  }
  yumrepo {"alma-extras":
    baseurl  => "http://${mirrorserver}/almalinux/${majorver}/extras/\$basearch/os/",
    descr    => 'AlmaLinux $releasever - Extras',
    enabled  => 1,
    gpgcheck => 0,
    priority => 20,
    before   => Yumrepo['site']
  }
  if $operatingsystemmajrelease >= '9' {
  yumrepo {"alma-crb":
    baseurl  => "http://${mirrorserver}/almalinux/${majorver}/CRB/\$basearch/os/",
    descr    => 'AlmaLinux $releasever - CRB',
    enabled  => 1,
    gpgcheck => 0,
    priority => 20,
    before   => Yumrepo['site']
  }
  }
  else {
    yumrepo {"alma-powertools":
    baseurl  => "http://${mirrorserver}/almalinux/${majorver}/PowerTools/\$basearch/os/",
    descr    => 'AlmaLinux $releasever - PowerTools',
    enabled  => 1,
    gpgcheck => 0,
    priority => 20,
    before   => Yumrepo['site']
  }
  }
  if $epelgrid {
    yumrepo { "epel":
      baseurl => "http://${mirrorserver}/yum/epel_grid/${majorver}/Everything/\$basearch",
      descr => "epel",
      metalink => absent,
      enabled => 1,
      gpgcheck => 0,
      priority => 40,
      before   => Yumrepo['site']
    }
  } elsif $epelyum{
    yumrepo { "epel":
      baseurl => "http://${mirrorserver}/yum/epel/${operatingsystemmajrelease}/Everything/\$basearch",
      descr => "epel",
      metalink => absent,
      enabled => 1,
      gpgcheck => 0,
      priority => 40,
      before   => Yumrepo['site']
    }
  }  else {
    yumrepo { "epel":
      baseurl => "http://${mirrorserver}/epel/${operatingsystemmajrelease}/Everything/\$basearch",
      descr => "epel",
      metalink => absent,
      enabled => 1,
      gpgcheck => 0,
      priority => 40,
      before   => Yumrepo['site']
    }
  }
  package{"dnf-automatic":
    ensure  => installed,
    require  => Yumrepo['site']
  }-> 
  augeas{"apply_updates" :
    context => "/files/etc/dnf/automatic.conf/commands",
    changes => "set  apply_updates $apply_updates",
  }-> 
  #file {"/etc/dnf/automatic.conf":
  #  group      =>  'root',
  #  source     =>  "puppet:///modules/${module_name}/automatic.conf",
  #}->
  if $yumautoupdate {
     service{"dnf-automatic.timer":
       ensure      => true,
       enable      => true,
       hasstatus   => true,
     }
  }else {
     service{"dnf-automatic.timer":
       ensure      => false,
       enable      => false,
       hasstatus   => true,
     }
  }
}
/CentOS|centos/:{
  yumrepo {"CentOS-base":
    baseurl  => "http://${mirrorserver}/yum/${centos_media}/${operatingsystemrelease}/os/\$basearch",
    descr    => "centos-base",
    enabled  => 1,
    gpgcheck => 0,
    priority => 20,
    before   => Yumrepo['site']
  }
  #if $yumautoupdate {
    yumrepo {"CentOS-update":
      baseurl  => "http://${mirrorserver}/yum/${centos_media}/${operatingsystemrelease}/updates/\$basearch",
      descr    => "centos-updates",
      enabled  => 1,
      gpgcheck => 0,
      priority => 20,
      before   => Yumrepo['site']
    }
  #}
  yumrepo {"CentOS-extras":
    baseurl  => "http://${mirrorserver}/yum/${centos_media}/${operatingsystemrelease}/extras/\$basearch",
    descr    => "centos-base",
    enabled  => 1,
    gpgcheck => 0,
    priority => 20,
    before   => Yumrepo['site']
  }
  if $operatingsystemmajrelease >= '6' {
    if  ($facts['os']['architecture'] == "x86_64") or ($facts['os']['release']['major'] >= '7') {
      yumrepo { "SCLo":
        baseurl => "http://${mirrorserver}/yum/${centos_media}/${operatingsystemrelease}/sclo/\$basearch/sclo",
        descr => "Software Collections",
        enabled => 1,
        gpgcheck => 0,
        priority => 20,
        before   => Yumrepo['site'],
      }
    }
    yumrepo { "SCLrh":
      baseurl => "http://${mirrorserver}/yum/${centos_media}/${operatingsystemrelease}/sclo/\$basearch/rh",
      descr => "Software Collections rh",
      enabled => 1,
      gpgcheck => 0,
      priority => 20,
      before   => Yumrepo['site'],
    }
  }

  if $epelgrid {
    yumrepo { "epel":
      baseurl => "http://${mirrorserver}/yum/epel_grid/${operatingsystemmajrelease}/\$basearch",
      descr => "epel",
      metalink => absent,
      enabled => 1,
      gpgcheck => 0,
      priority => 40,
      before   => Yumrepo['site']
    }
  } elsif $epelyum{
    yumrepo { "epel":
      baseurl => "http://${mirrorserver}/yum/epel/${operatingsystemmajrelease}/\$basearch",
      descr => "epel",
      metalink => absent,
      enabled => 1,
      gpgcheck => 0,
      priority => 40,
      before   => Yumrepo['site']
    }
  }  else {
    yumrepo { "epel":
      baseurl => "http://${mirrorserver}/epel/${operatingsystemmajrelease}/\$basearch",
      descr => "epel",
      metalink => absent,
      enabled => 1,
      gpgcheck => 0,
      priority => 40,
      before   => Yumrepo['site']
    }
  }

  if $enable_debug {
    yumrepo {"Centos-debuginfo":
      baseurl  => "http://${mirrorserver}/yum/centos-debuginfo/${operatingsystemmajrelease}/\$basearch",
      descr    => "centos-updates",
      enabled  => 1,
      gpgcheck => 0,
      priority => 20,
      before   => Yumrepo['site']
    }
  }
  package {"yum-plugin-priorities":
    ensure   => 'installed',
    before   => Yumrepo['site'],
  }
  package {"yum-cron":
    ensure   => 'installed',
    before   => Yumrepo['site'],
  } ->
  augeas{"apply_updates" :
    context => "/files/etc/yum/yum-cron.conf/commands",
    changes => "set  apply_updates $apply_updates",
  } ->
  file { "/etc/cron.daily/0yum-daily.cron":
    mode       =>  '755',
    owner      =>  'root',
    group      =>  'root',
    source     =>  "puppet:///modules/yumlocal/0yum-daily.cron",
  } ->

  if $yumautoupdate {
    service { 'yum-cron':
      name       =>  'yum-cron',
      ensure     =>  'running',
      enable     =>   true,
      hasstatus  =>   true,
      hasrestart =>   true,
      require    =>   Package['yum-cron'],
    }
  }
  else{
    service { 'yum-cron':
      name       =>  'yum-cron',
      ensure     =>  'stopped',
      enable     =>   false,
      hasstatus  =>   true,
      hasrestart =>   true,
      require    =>   Package['yum-cron'],
    }
  }
  augeas{"yum_exclude" :
    context => "/files/etc/yum/yum-cron.conf/base",
    changes => [
      "set  exclude[1]  kernel*",
      "set  exclude[2]  openafs*",
      "set  exclude[3]  *-kmdl-*",
      "set  exclude[4]  kmod-*",
      "set  exclude[5]  *firmware*",
      "set  exclude[6]  python-perf",
    ]
  }
}
/scientific|Scientific|redhat|RedHat|Redhat/:{
  if $epelgrid {
    yumrepo { "epel":
      baseurl => "http://${mirrorserver}/yum/epel_grid/${operatingsystemmajrelease}/\$basearch",
      descr => "epel",
      metalink => absent,
      enabled => 1,
      gpgcheck => 0,
      priority => 40,
      before   => Yumrepo['site']
    }
  }
  else {
    yumrepo { "epel":
      baseurl => "http://${mirrorserver}/yum/epel/${operatingsystemmajrelease}/\$basearch",
      descr => "epel",
      metalink => absent,
      enabled => 1,
      gpgcheck => 0,
      priority => 40,
      before   => Yumrepo['site']
    }
  }

  case $operatingsystemmajrelease {
  '6': {
     file{"/etc/yum.repos.d/sl.repo":
       ensure   => absent,
     }
     file{"/etc/yum.repos.d/sl6x.repo":
       ensure   => absent,
     }
     file {'/etc/yum.repos.d/SL-other.repo':
       ensure   => "absent",
     }

    if $nminorrelease {
      yumrepo { "SL-base":
        baseurl  => "http://${mirrorserver}/yum/scientific/${operatingsystemmajrelease}/x86_64/os",
        descr    => "SL-base",
        enabled  => 1,
        gpgcheck => 0,
        priority => 20,
        before   => Yumrepo['site']
      }
      #if $enable_debug {
      #  #yumrepo { "SL-debug":
      #  #  baseurl  => "http://${mirrorserver}/yum/scientific/${operatingsystemmajrelease}/archive/debuginfo",
      #  #  descr    => "SL-debug",
      #  #  enabled  => 1,
      #  #  gpgcheck => 0,
      #  #  priority => 20,
      #  #  before   => Yumrepo['site']
      #  #}
      #}
      yumrepo { "SL-security":
        baseurl => "http://${mirrorserver}/yum/scientific/${operatingsystemmajrelease}/x86_64/updates/security",
        descr => "SL-secrtity",
        enabled => 1,
        gpgcheck => 0,
        priority => 20,
        before   => Yumrepo['site'],
      }
      yumrepo { "SL-fastbugs":
        baseurl => "http://${mirrorserver}/yum/scientific/${operatingsystemmajrelease}/x86_64/updates/fastbugs",
        descr => "SL-fastbugs",
        enabled => 1,
        gpgcheck => 0,
        priority => 20,
        before   => Yumrepo['site'],
      }
      package {"yum-autoupdate":
        ensure   => 'installed',
        before   => Yumrepo['site'],
      }
      package {"yum-plugin-priorities":
        ensure   => 'installed',
        before   => Yumrepo['site'],
      }
      #yumrepo { "devtoolset":
      #  baseurl => "http://${mirrorserver}/yum/scientific/${operatingsystemmajrelease}/external_products/devtoolset/x86_64",
      #  descr => "Scientific Linux devtoolset",
      #  enabled => 1,
      #  gpgcheck => 0,
      #  priority => 20,
      #  before   => Yumrepo['site'],
      #}
    }

    else {
      yumrepo { "SL-base":
        baseurl  => "http://${mirrorserver}/yum/scientific/${operatingsystemrelease}/x86_64/os",
        descr    => "SL-base",
        enabled  => 1,
        gpgcheck => 0,
        priority => 20,
        before   => Yumrepo['site']
      }
      #if $enable_debug {
      #  #yumrepo { "SL-debug":
      #  #  baseurl  => "http://${mirrorserver}/yum/scientific/${operatingsystemrelease}/archive/debuginfo",
      #  #  descr    => "SL-debug",
      #  #  enabled  => 1,
      #  #  gpgcheck => 0,
      #  #  priority => 20,
      #  #  before   => Yumrepo['site']
      #  #}
      #}
      yumrepo { "SL-security":
        baseurl => "http://${mirrorserver}/yum/scientific/${operatingsystemrelease}/x86_64/updates/security",
        descr => "SL-secrtity",
        enabled => 1,
        gpgcheck => 0,
        priority => 20,
        before   => Yumrepo['site']
      }
      yumrepo { "SL-fastbugs":
        baseurl => "http://${mirrorserver}/yum/scientific/${operatingsystemrelease}/x86_64/updates/fastbugs",
        descr => "SL-fastbugs",
        enabled => 1,
        gpgcheck => 0,
        priority => 20,
        before   => Yumrepo['site'],
      }
      #yumrepo { "devtoolset":
      #  baseurl => "http://${mirrorserver}/yum/scientific/${operatingsystemmajrelease}/external_products/devtoolset/x86_64",
      #  descr => "Scientific Linux devtoolset",
      #  enabled => 1,
      #  gpgcheck => 0,
      #  priority => 20,
      #  before   => Yumrepo['site'],
      #}
      package {"yum-plugin-priorities":
        ensure   => 'installed',
        before   => Yumrepo['site'],
      }
      package {"yum-autoupdate":
        ensure   => 'installed',
        before   => Yumrepo['site'],
      }
    }
    if $yumautoupdate{
      augeas{"yum-autoupdate" :
        context => "/files/etc/sysconfig/yum-autoupdate/",
        changes => [
          'set ENABLED \'"true"\'',
          'set PRERUN \'"yum clean all"\'',
        ],
        require => Package["yum-autoupdate"],
      }
    }
    else{
      augeas{"yum-autoupdate" :
        context => "/files/etc/sysconfig/yum-autoupdate/",
        changes => 'set ENABLED \'"false"\'',
        require => Package["yum-autoupdate"],
      }
    }
  }
  '7':{
     file{"/etc/yum.repos.d/sl7.repo":
       ensure   => absent,
     }
     file{"/etc/yum.repos.d/sl7-testing.repo":
       ensure   => absent,
     }
     file{"/etc/yum.repos.d/sl7-other.repo":
       ensure   => absent,
     }
     file{"/etc/yum.repos.d/sl7-fastbugs.repo":
       ensure   => absent,
     }
     file{"/etc/yum.repos.d/sl7-rolling.repo":
       ensure   => absent,
     }
     file{"/etc/yum.repos.d/sl7-security.repo":
       ensure   => absent,
     }



    if $nminorrelease{
      yumrepo { "SL-base":
        baseurl  => "http://${mirrorserver}/yum/scientific/${operatingsystemmajrelease}/x86_64/os",
        descr    => "SL-base",
        enabled  => 1,
        gpgcheck => 0,
        priority => 20,
        before   => Yumrepo['site']
      }
      #if $enable_debug {
      #  yumrepo { "SL-debug":
      #    baseurl  => "http://${mirrorserver}/yum/scientific/${operatingsystemmajrelease}/archive/debuginfo",
      #    descr    => "SL-debug",
      #    enabled  => 1,
      #    gpgcheck => 0,
      #    priority => 20,
      #    before   => Yumrepo['site']
      #  }
      #}
      yumrepo { "SL-security":
        baseurl => "http://${mirrorserver}/yum/scientific/${operatingsystemmajrelease}/x86_64/updates/security",
        descr => "SL-secrtity",
        enabled => 1,
        gpgcheck => 0,
        priority => 20,
        before   => Yumrepo['site'],
      }
      yumrepo { "SL-fastbugs":
        baseurl => "http://${mirrorserver}/yum/scientific/${operatingsystemmajrelease}/x86_64/updates/fastbugs",
        descr => "SL-fastbugs",
        enabled => 1,
        gpgcheck => 0,
        priority => 20,
        before   => Yumrepo['site'],
      }
      cron { 'clean_sl7_repo':
        command => 'rm -f /etc/yum.repos.d/sl7*',
        user    => 'root',
        hour    => 11,
        minute  => 30
      }
    }
    else {
      yumrepo { "SL-base":
        baseurl  => "http://${mirrorserver}/yum/scientific/${operatingsystemrelease}/x86_64/os",
        descr    => "SL-base",
        enabled  => 1,
        gpgcheck => 0,
        priority => 20,
        before   => Yumrepo['site']
      }
      #if $enable_debug {
      #  yumrepo { "SL-debug":
      #    baseurl  => "http://${mirrorserver}/yum/scientific/${operatingsystemrelease}/archive/debuginfo",
      #    descr    => "SL-debug",
      #    enabled  => 1,
      #    gpgcheck => 0,
      #    priority => 20,
      #    before   => Yumrepo['site']
      #  }
      #}
      yumrepo { "SL-security":
        baseurl => "http://${mirrorserver}/yum/scientific/${operatingsystemrelease}/x86_64/updates/security",
        descr => "SL-secrtity",
        enabled => 1,
        gpgcheck => 0,
        priority => 20,
        before   => Yumrepo['site']
      }
      yumrepo { "SL-fastbugs":
        baseurl => "http://${mirrorserver}/yum/scientific/${operatingsystemrelease}/x86_64/updates/fastbugs",
        descr => "SL-fastbugs",
        enabled => 1,
        gpgcheck => 0,
        priority => 20,
        before   => Yumrepo['site']
      }
    }
    package {"yum-plugin-priorities":
      ensure   => 'installed',
      before   => Yumrepo['site'],
    }
    package {"yum-cron":
      ensure   => 'installed',
      before   => Yumrepo['site'],
    } ->
    augeas{"apply_updates" :
      context => "/files/etc/yum/yum-cron.conf/commands",
      changes => "set  apply_updates $apply_updates",
    }


  }
  '5': {
    $osmediadir = delete($operatingsystemrelease,'.')
    yumrepo { "SL-base":
      baseurl => "http://${mirrorserver}/yum/scientific/${osmediadir}/x86_64/SL",
      descr => "SL-base",
      enabled => 1,
      gpgcheck => 0,
      priority => 20,
      before   => Yumrepo['site']
    }
    yumrepo { "SL-security":
      baseurl => "http://${mirrorserver}/yum/scientific/${osmediadir}/x86_64/updates/security",
      descr => "SL-secrtity",
      enabled => 1,
      gpgcheck => 0,
      priority => 20,
      before   => Yumrepo['site']
    }
    yumrepo { "repoforge":
      baseurl => "http://${mirrorserver}/repoforge/redhat/el5/en/x86_64/rpmforge/",
      descr => "repoforge",
      enabled => 1,
      gpgcheck => 0,
      priority => 40,
      before   => Yumrepo['site']
    }

    package {"yum-autoupdate":
      ensure   => 'installed'
    }
    #yumrepo { "dag":
    #  name    =>  'dag',
    #  ensure  =>  absent,
    # }
    if $yumautoupdate{
      service { 'yum':
        name       =>  'yum',
        ensure     =>  'running',
        enable     =>   true,
        hasstatus  =>   true,
        hasrestart =>   true,
        require    =>   Package['yum-autoupdate'],
      }
    }
    else{
      service { 'yum':
        name       =>  'yum',
        ensure     =>  'stopped',
        enable     =>   false,
        hasstatus  =>   true,
        hasrestart =>   true,
        require    =>   Package['yum-autoupdate'],
      }
    }
    package {"yum-priorities":
      ensure   => 'installed',
      before   => Yumrepo['site'],
    }
  }
  }
 }}


  yumrepo { "puppet7":
    baseurl => "http://${mirrorserver}/yum/puppet/puppet7/el/${operatingsystemmajrelease}/\$basearch",
    descr => "puppet7",
    enabled => 1,
    gpgcheck => 0,
    priority => 40,
    before   => Yumrepo['site']
  }
#  yumrepo { "site":
#    baseurl => "http://${mirrorserver}/yum/site/${facts['os']['release']['major']}/\$basearch",
#    descr => "site",
#    enabled => 1,
#    gpgcheck => 0,
#    priority => $sitepriority,
#  }
  file {'/etc/yum.repos.d/system.repo':
    ensure   => "absent",
  }
}

class csns::dgyumlocal::repos_umd (
  $umd_priority = 40
)inherits csns::dgyumlocal{

  case $facts['os']['release']['major'] {
  '5': {
     yumrepo { "EMI_3_base":
        baseurl => "http://${mirrorserver}/yum/emi/3/sl5/x86_64/base",
        descr   => "EMI_3_base",
        enabled => 1,
        gpgcheck => 0,
        priority => 40,
        before   => Yumrepo['site']
     }
     yumrepo { "EMI_3_contribs":
        baseurl => "http://${mirrorserver}/yum/emi/3/sl5/x86_64/contribs",
        descr   => "EMI_3_contribs",
        enabled => 1,
        gpgcheck => 0,
        priority => 40,
        before   => Yumrepo['site']
     }
     yumrepo { "EMI_3_updates":
        baseurl => "http://${mirrorserver}/yum/emi/3/sl5/x86_64/updates",
        descr   => "EMI_3_updates",
        enabled => 1,
        gpgcheck => 0,
        priority => 40,
        before   => Yumrepo['site']
     }
     yumrepo { "wlcg":
        baseurl => "http://${mirrorserver}/yum/wlcg/sl$facts['os']['release']['major']/x86_64",
        descr   => "wlcg",
        enabled => 1,
        gpgcheck => 0,
        priority => 43,
        before   => Yumrepo['site']
     }
   }
  '6': {
     #yumrepo { "UMD_4_base":
     #   baseurl => "http://${mirrorserver}/yum/umd/4/sl6/x86_64/base",
     #   descr   => "UMD_4_base",
     #   enabled => 1,
     #   gpgcheck => 0,
     #   exclude  =>'cvmfs-config-egi',
     #   priority => $umd_priority,
     #   before   => Yumrepo['site']
     #}
     #yumrepo { "UMD_4_updates":
     #   baseurl => "http://${mirrorserver}/yum/umd/4/sl6/x86_64/updates",
     #   descr   => "UMD_4_updates",
     #   enabled => 1,
     #   gpgcheck => 0,
     #   exclude  =>'cvmfs-config-egi',
     #   priority => $umd_priority,
     #   before   => Yumrepo['site']
     #}
     yumrepo { "wlcg":
        baseurl => "http://${mirrorserver}/yum/wlcg/sl$facts['os']['release']['major']/x86_64",
        descr   => "wlcg",
        enabled => 1,
        gpgcheck => 0,
        priority => 33,
        before   => Yumrepo['site']
     }
  }
  '7': {
     yumrepo { "UMD_4_base":
        baseurl => "http://${mirrorserver}/yum/umd/4/centos7/x86_64/base",
        descr   => "UMD_4_base",
        enabled => 1,
        gpgcheck => 0,
	exclude  =>'cvmfs-config-egi',
        priority => $umd_priority,
        before   => Yumrepo['site']
     }
     yumrepo { "UMD_4_updates":
        baseurl => "http://${mirrorserver}/yum/umd/4/centos7/x86_64/updates",
        descr   => "UMD_4_updates",
        enabled => 1,
        gpgcheck => 0,
	exclude  =>'cvmfs-config-egi',
        priority => $umd_priority,
        before   => Yumrepo['site']
     }
     yumrepo { "wlcg":
        baseurl => "http://${mirrorserver}/yum/wlcg/centos7/x86_64",
        descr   => "wlcg",
        enabled => 1,
        gpgcheck => 0,
        priority => 33,
        before   => Yumrepo['site']
     }
  }
 '9': {
     yumrepo { "wlcg":
        baseurl => "http://${mirrorserver}/yum/wlcg/el9/\$basearch",
        descr   => "wlcg",
        enabled => 1,
        gpgcheck => 0,
        priority => 33,
        before   => Yumrepo['site']
     }
  }
  }

   yumrepo { "EGI-trustanchors":
      baseurl => "http://${mirrorserver}/yum/ca",
      descr   => "EGI-trustanchors",
      enabled => 1,
      gpgcheck => 0,
      priority => 43,
      before   => Yumrepo['site']
   }
}

